#include "Capply.hpp"
#include "pre-deal.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>

#define LOCALHOST "127.0.0.1"
#define PORT 3456

using namespace std;

int main(){

    client hjj(LOCALHOST,PORT);

     //连接服务器
     while(!hjj.apply()){
        cout<< "连接错误,按ESC退出,按其他任意键重试!" << endl;
        if( waitKey() == 27 ){
            cout<< "连接终止!" << endl;
            return -1;
        }
        else
            continue;
     }


    //捕获图片
    bool flag = false;
    VideoCapture capture;
    Mat cameraFeed;
    capture.open(0);  // 打开摄像头
    if (!capture.isOpened()) {
        cerr << "无法打开摄像头" << endl;
        return -1;
    }

    bool running = true;
    while(running){
        if(!capture.read(cameraFeed)) { // 读取摄像头捕获的图像帧
            cout << " 摄像头终止 " << endl;
            return -1;
        }
        if(!checkLighting(cameraFeed)){
            namedWindow( "Client", CV_MINOR_VERSION);// 创建一个窗口用于显示图像
            imshow( "Client", cameraFeed);   // 在窗口中显示图像
            if(waitKey() == 27){
                cout << "环境太差,已退出" << endl;
                return -1;
            }
            else
                continue;
        }
        hjj.setimg(cameraFeed);
        hjj.sendimg();
        if(waitKey(8) == 27){
            cout<< "识别完成，谢谢使用!" << endl;
            return 0;
        }
    }
    hjj.closefd();
    return 0;
}